# @Version        : 1.0
# @Update Time    : 2025/8/16 20:35
# @File           : __init__.py.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息

from fastapi import FastAPI
from starlette.staticfiles import StaticFiles

from applications.config import settings
from applications.core.exception_handler import register_exception
from applications.core.middlewares import register_middleware
from applications.core.register import on_lifespan
from applications.view import init_routes


def create_app():
    app = FastAPI(
        debug=settings.DEBUG,
        title=settings.SYSTEM_NAME,
        description=settings.SYSTEM_DESC,
        version=settings.SYSTEM_VERSION,
        contact={
            "name": settings.SYSTEM_CONTACT_NAME,
            "email": settings.SYSTEM_CONTACT_EMAIL,
        },
        license_info={
            "name": settings.SYSTEM_LICENSE_NAME,
            "url": settings.SYSTEM_LICENSE_URL,
        },
        terms_of_service=settings.SYSTEM_TERMS_OF_SERVICE,
        servers=settings.SYSTEM_SERVERS,
        openapi_url=settings.SYSTEM_OPENAPI_URL,
        docs_url=settings.SYSTEM_DOCS_URL,
        redoc_url=settings.SYSTEM_REDOC_URL,
        swagger_ui_parameters={
                "docExpansion": "none",  # 默认全部折叠
                "defaultModelExpandDepth": 1,
                "filter": True,  # 启用搜索
                "persistAuthorization": True,  # 保持认证
                "tagsSorter": "alpha",  # 按字母排序
                "displayRequestDuration": True,  # 显示请求时长
                "syntaxHighlight": {"theme": "obsidian"},
                # 语法高亮["agate"*,"arta","monokai","nord","obsidian","tomorrow-night","idea"]
                # "dom_id":"#swagger-ui",#添加自定义ID
                # "layout":"BaseLayout",#默认布局
                # "deepLinking":True,#启用深度链接
                # "showExtensions":True,#显示扩展
                # "showCommonExtensions":True,#显示常用扩展
            },
        lifespan=on_lifespan,
    )
    # 中间件注册
    register_middleware(app)
    # 静态文件挂载
    mount_static_file(app)
    # 路由注册
    init_routes(app)
    # 异常注册
    register_exception(app)

    return app


def mount_static_file(app: FastAPI):
    app.mount("/static", StaticFiles(directory="static"), name="static")
